home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / Book Chapters / 02 - Basic Game Graphics / Amalgam ƒ / Menu.c < prev    next >
Encoding:
Text File  |  1995-03-05  |  8.0 KB  |  356 lines  |  [TEXT/MMCC]

  1. //\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
  2. //
  3. //    Menu.c
  4. //
  5. //    Handles menu stuff.
  6. //
  7. //    History:
  8. //
  9. //    950305 jb: Written
  10. //
  11. //\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
  12.  
  13.  
  14. //  __#Defines________________________________________________________________________
  15. #define kAmalgamMBARID            128
  16. #define kNumMenus                3
  17.  
  18. //  __#Headers________________________________________________________________________
  19. #include "Amalgam.h"
  20. #include "Menu.h"
  21.  
  22. //  __#Protos_________________________________________________________________________
  23. //  __ Macros_________________________________________________________________________
  24. //  __ Enums__________________________________________________________________________
  25. typedef enum {
  26.     eAppleResID = 400,
  27.     eFileResID,
  28.     eEditResID
  29. } etMenuResIDs;
  30.  
  31. typedef enum {
  32.     eAppleMenuNum = 0,
  33.     eFileMenuNum,
  34.     eEditMenuNum
  35. } etMenuIndices;
  36.  
  37. typedef enum {
  38.     eQuit = 1
  39. } etFileItems;
  40.  
  41. //  __ Typedefs_______________________________________________________________________
  42. //  __ Static Protos__________________________________________________________________
  43. //  __ Extern Globals_________________________________________________________________
  44. Boolean                    gUserWantsMenuBar = TRUE;    //used to restore menubar state
  45.                                                     //on a resume
  46.  
  47. //  __ Static Globals_________________________________________________________________
  48. static MenuHandle        menuHdlArray[kNumMenus];        //array of our menus
  49.  
  50. static Boolean            menuBarHidden;                    //current state of menubar
  51. static Rect                menuBarRect;
  52. static short            menuBarHeight;
  53. static RgnHandle        savedGrayRgn;
  54. static RgnHandle        newGrayRgn;
  55. static RgnHandle        coveredRgn;
  56.  
  57. //  __ Functions______________________________________________________________________
  58.  
  59.  
  60.  
  61. //____ InitOurMenubar __________________________________________________________________________
  62. //
  63. //    Description
  64. //
  65. //    Returns    void
  66. //
  67. void InitOurMenubar( void )
  68. {
  69. Handle            menuBarhdl;
  70. short            x;
  71.     
  72.     menuBarhdl = GetNewMBar( kAmalgamMBARID );
  73.     SetMenuBar(menuBarhdl);
  74.  
  75.     for (x=0; x < kNumMenus; x++)
  76.         menuHdlArray[x] = GetMHandle(eAppleResID + x);
  77.  
  78.     AddResMenu(menuHdlArray[eAppleMenuNum], 'DRVR');        //Add Apple Menu items
  79.  
  80.     EnableItem(menuHdlArray[eAppleMenuNum], eQuit);        //well, this is ALWAYS enabled…
  81.  
  82.     DrawMenuBar();
  83.     
  84.     gUserWantsMenuBar = TRUE;
  85.     menuBarHidden = FALSE;
  86.  
  87. }//InitOurMenubar
  88.  
  89.  
  90.  
  91.  
  92. //____ HandleMenuChoice __________________________________________________________________________
  93. //
  94. //    Our menus are pretty simple, so this doesn't have to do too much.
  95. //
  96. //    Returns    void
  97. //
  98. void HandleMenuChoice( long menuChoice )
  99. {
  100. short        theMenu;
  101. short        theItem;
  102. Str255        aStr;
  103.  
  104.     if (0 != menuChoice)
  105.     {    
  106.         theMenu = HiWord(menuChoice);
  107.         theItem = LoWord(menuChoice);
  108.         HiliteMenu(theMenu);
  109.  
  110.         switch(theMenu)
  111.         {
  112.             case eAppleResID:
  113.                 // we don't have an About, so bleep!
  114.                 if (theItem < 2)
  115.                 {
  116.                     SysBeep(1);
  117.                     return;
  118.                 }
  119.                 // handle Apple Menu items
  120.                 GetItem(menuHdlArray[eAppleMenuNum], theItem, aStr);
  121.                 OpenDeskAcc(aStr);
  122.                 break;
  123.             case eFileResID:
  124.                 if (eQuit == theItem)
  125.                 {
  126.                     gQuitting = TRUE;    //tell the world the user wants to quit
  127.                 }
  128.                 break;
  129.             case eEditResID:
  130.                 break;
  131.             default:
  132.                 break;
  133.         }
  134.         
  135.         HiliteMenu(0);
  136.         CheckMenuHide(NULL);    //re-hide menubar if it was shown
  137.     }
  138. }    //    HandleMenuChoice
  139.  
  140.  
  141.  
  142.  
  143. //____ ToggleMenuBar __________________________________________________________________________
  144. //
  145. //    Description
  146. //
  147. //    Returns    void
  148. //
  149. void ToggleMenuBar( Boolean showIt )
  150. {
  151.     //handle redundancies
  152.     if (showIt && !menuBarHidden)
  153.         return;
  154.         
  155.     if (!showIt && menuBarHidden)
  156.         return;
  157.     
  158.     //user is actually changing state of menubar.
  159.     if (showIt)
  160.     {
  161.         gUserWantsMenuBar = TRUE;
  162.         ShowMenuBar();
  163.     }
  164.     else
  165.     {
  166.         gUserWantsMenuBar = FALSE;
  167.         HideMenuBar();
  168.     }
  169.     
  170. }//ToggleMenuBar
  171.  
  172.  
  173. #pragma mark -
  174.  
  175.  
  176. //____ StartupHideMenuBar __________________________________________________________________________
  177. //
  178. //    Call this very early in your startup cycle; it saves off important information
  179. //    about the menubar.
  180. //
  181. void StartupHideMenuBar( void )
  182. {
  183. GDHandle        mainDev;
  184.  
  185.     savedGrayRgn = LMGetGrayRgn(); // Store the old gray region
  186.     menuBarHeight = LMGetMBarHeight(); // Store the menu bar height
  187.  
  188.     //determine rectangle bounding the menubar so we can tell if
  189.     //user has mouse-downed in it later
  190.     mainDev = GetMainDevice();
  191.     menuBarRect = (**mainDev).gdRect;
  192.     menuBarRect.bottom = menuBarHeight;
  193.     
  194. }//StartupHideMenuBar
  195.  
  196.  
  197. //____ ShutdownHideMenuBar __________________________________________________________________________
  198. //
  199. //
  200. //
  201. void ShutdownHideMenuBar( void )
  202. {
  203.     LMSetGrayRgn( savedGrayRgn );    // Restore the gray region !
  204. }//ShutdownHideMenuBar
  205.  
  206.  
  207. //____ HideMenuBar __________________________________________________________________________
  208. //
  209. //
  210. //
  211. void HideMenuBar( void )
  212. {
  213. GDHandle        mainDisplay;
  214. Rect            mainDisplayBounds;
  215. RgnHandle        workRgn;
  216. GrafPtr            windowPort;
  217. GrafPtr            oldPort;
  218. CGrafPtr        windowCPort;
  219. WindowPtr        frontWindow;
  220.     
  221.     // Make sure the menu bar is not already hidden
  222.     if ( menuBarHidden )
  223.     {
  224.         return;
  225.     }
  226.     
  227.     // Get the rectangle for the main display
  228.     mainDisplay = GetMainDevice();
  229.     mainDisplayBounds = ( *mainDisplay )->gdRect;
  230.     
  231.     // Calculate the new regions
  232.     workRgn = NewRgn();
  233.     RectRgn( workRgn, &mainDisplayBounds );
  234.     newGrayRgn = NewRgn();
  235.     coveredRgn = NewRgn();
  236.     UnionRgn( workRgn, savedGrayRgn, newGrayRgn );
  237.     DiffRgn( newGrayRgn, savedGrayRgn, coveredRgn );
  238.     DisposeRgn( workRgn );
  239.     
  240.     // Set the new gray region
  241.     LMSetGrayRgn( newGrayRgn );
  242.     
  243.     // Get the window manager port clip region
  244.     GetPort( &oldPort );
  245.     GetWMgrPort( &windowPort );
  246.     SetPort( windowPort );
  247.     SetClip( newGrayRgn );
  248.     
  249.     GetCWMgrPort( &windowCPort );
  250.     SetPort( ( GrafPtr )windowCPort );
  251.     SetClip( newGrayRgn );
  252.     
  253.     // Force an update of the desktop
  254.     PaintOne( nil, newGrayRgn );
  255.     
  256.     // Update the windows
  257.     frontWindow = FrontWindow();
  258.     PaintOne( ( WindowRef )frontWindow, coveredRgn );
  259.     PaintBehind( ( WindowRef )frontWindow, coveredRgn );
  260.     CalcVis( ( WindowRef )frontWindow );
  261.     CalcVisBehind( ( WindowRef )frontWindow, coveredRgn );
  262.     
  263.     // Mark the menu bar as hidden
  264.     LMSetMBarHeight( 0 );
  265.     menuBarHidden = true;
  266.     
  267.     // Restore the port
  268.     SetPort( oldPort );
  269.     
  270. }//HideMenuBar
  271.  
  272.  
  273. //____ ShowMenuBar __________________________________________________________________________
  274. //
  275. //
  276. //
  277. void ShowMenuBar( void )
  278. {
  279. WindowPtr    frontWindow;
  280. GrafPtr        windowPort;
  281. GrafPtr        oldPort;
  282. CGrafPtr    windowCPort;
  283.     
  284.     // Make sure the menu bar is hidden
  285.     if ( !menuBarHidden )
  286.     {
  287.         return;
  288.     }
  289.     
  290.     // Reset the menu bar height
  291.     LMSetMBarHeight( menuBarHeight );
  292.     
  293.     // Restore the gray region
  294.     LMSetGrayRgn( savedGrayRgn );
  295.     
  296.     // Recalculate the windows
  297.     frontWindow = FrontWindow();
  298.     CalcVis( ( WindowRef )frontWindow );
  299.     CalcVisBehind( ( WindowRef )frontWindow, newGrayRgn );
  300.     
  301.     // Reset the window manager port's clipping
  302.     GetPort( &oldPort );
  303.     GetWMgrPort( &windowPort );
  304.     SetPort( windowPort );
  305.     SetClip( newGrayRgn );
  306.     GetCWMgrPort( &windowCPort );
  307.     SetPort( ( GrafPtr )windowCPort );
  308.     SetClip( newGrayRgn );
  309.     
  310.     // Erase the menu region
  311.     FillRgn( coveredRgn, &qd.white );
  312.     SetPort( oldPort );
  313.     
  314.     // Redraw the menu bar
  315.     HiliteMenu( 0 );
  316.     DrawMenuBar();
  317.     
  318.     // Mark that the menu bar is not hidden
  319.     menuBarHidden = FALSE;
  320. }//ShowMenuBar
  321.  
  322.  
  323.  
  324. //____ CheckMenuHide __________________________________________________________________________
  325. //
  326. //    This unhides or hides them menubar in certain situations:
  327. //
  328. //    o    Mousedown in region where menubar would be - we show menu bar
  329. //    o    we're no longer the Foreground application - we show menu bar
  330. //    o    Mousedown _not_ in menubar region - we may hide the menubar
  331. //
  332. //
  333. //    Call the when there's a context switch, or there's a mouse-down;
  334. //    Furthermore, call this after handling mouse-menu action to re-hide
  335. //    the menu bar;
  336. //
  337. //    Pass in mouseDowns in global coordinates if there was a mousedown,
  338. //    otherwise, pass NULL
  339. //
  340. void CheckMenuHide( Point *mouseWhere )
  341. {
  342.     if ((gInForeground == FALSE) || ((NULL != mouseWhere ) && (PtInRect(*mouseWhere, &menuBarRect))))
  343.     {
  344.         ShowMenuBar();
  345.     }
  346.     else
  347.     {
  348.         if (!gUserWantsMenuBar)
  349.             HideMenuBar();
  350.         else
  351.             ShowMenuBar();
  352.     }
  353. }//CheckMenuHide
  354.  
  355.  
  356.